function cartFn() {
  this.oCartBody = $(".cart_body")
  this.oCartItem = this.oCartBody.find(".cart_item")


  this.tellnumber = localStorage.getItem('ACTIVE_USER')
  // this.cartEmpty()
  this.cartShow()
  this.cartEvent()
  // this.num=$(".num").val()
}

cartFn.prototype.cartShow = function () {
  cart({
    tellnumber: this.tellnumber,
  }).then(res => {
    if (res.status) {
      let html = ""
      res.data.forEach(v => {
        html += ` <li class="item_detail">
    <div class="cart_choose">
      <input type="checkbox" class="one_choose">
      <span class="cart_img"><img src="${v.goods_imgs}" alt=""></span>
    </div>
    <div class="cart_product">
      <a href="">${v.goods_title}</a>
    </div>
    <div class="cart_sku">
      <p class="sku_color">${v.goods_color}</p>
      <p class="sku_spec">${v.goods_weight}</p>
    </div>
    <div class="cart_price">
      <p class="price">${v.goods_price}</p>
      <div class="tag_active">${v.goods_span}</div>
    </div>
    <div class="cart_num">
      <div class="num_change">
        <span class="reduce" goodsId="${v.goods_id}">
          -
        </span>
        <input type="text" class="num" value="${v.num}" index="${v.num}" goodsId="${v.goods_id}">
        <span class="add" goodsId="${v.goods_id}">
          +
        </span>
      </div>
      <div class="stock_quota">限购5件</div>
    </div>
    <p class="cart_total">${v.goods_price * v.num * 1}</p>
    <p class="item_delete" goodsId="${v.goods_id}">x</p>
  </li>`
      });
      this.oCartItem.html(html)
      this.carttotal()
    } else {
      this.cartEmpty()
    }
  })

}

cartFn.prototype.cartEvent = function () {
  this.oCartBody.click((e) => {
    const target = e.target
    //数量减
    if (target.classList.contains("reduce")) {
      if ($(target).next().val() == 1) {
        return
      } else {
        addcart({
          tellnumber: this.tellnumber,
          num: -1,
          id: $(target).attr("goodsId")
        }).then(res => {
          if (res.status) {
            let count = $(target).next().val() - 1
            $(target).next().val(count)
            this.carttotal()
          }
        })
      }
    }
    //数量加
    if (target.classList.contains("add")) {
      if ($(target).prev().val() >= 5) {
        return
      } else {
        addcart({
          tellnumber: this.tellnumber,
          num: 1,
          id: $(target).attr("goodsId")
        }).then(res => {
          if (res.status) {
            let count = $(target).prev().val() * 1 + 1
            $(target).prev().val(count)
            this.carttotal()
          }
        })
      }
    }

    //全选
    if (target.classList.contains("all_choose")) {
      for (var i = 0; i < $(".one_choose").length; i++) {
        if (target.checked == true) {
          $(".all_choose")[1].checked = true
          $(".all_choose")[0].checked = true
          $(".one_choose").parent().parent().css("background-color", "#FFFCEB")
        } else {
          $(".all_choose")[0].checked = false
          $(".all_choose")[1].checked = false
          $(".one_choose").parent().parent().css("background-color", "#fff")
        }

        $(".one_choose")[i].checked = $(".all_choose")[0].checked
        this.carttotal()
      }
    }

    //反选
    if (target.classList.contains("one_choose")) {
      let count = 0
      for (var i = 0; i < $(".one_choose").length; i++) {
        if (!$(".one_choose")[i].checked) {
          $($(".one_choose")[i]).parent().parent().css("background-color", "#fff")
        } else {
          $($(".one_choose")[i]).parent().parent().css("background-color", "#FFFCEB")
          count++
        }
      }
      $(".all_choose")[0].checked = count == $(".one_choose").length
      $(".all_choose")[1].checked = count == $(".one_choose").length
      this.carttotal()

    }

    //单删
    if (target.classList.contains("item_delete")) {
      delcart({
        tellnumber: this.tellnumber,
        id: $(target).attr("goodsId")
      }).then(res => {
        console.log(res);
        if (res.status) {
          $(target).parent().remove()
          //删除后重新判断全选
          for (var i = 0; i < $(".one_choose").length; i++) {
            if (!$(".one_choose")[i].checked) {
              break
            }
          }
          $(".all_choose")[0].checked = i == $(".one_choose").length
          $(".all_choose")[1].checked = i == $(".one_choose").length
          //购物车空
          this.carttotal()
          this.cartEmpty()
        }
      })
    }
    //批量删除
    if (target.classList.contains("all_delete")) {
      for (var i = 0; i < $(".one_choose").length; i++) {
        if ($(".one_choose")[i].checked) {
          delcart({
            tellnumber: this.tellnumber,
            id: $($(".num")[i]).attr("goodsId")
          }).then(res => {
            console.log(res);
            if (res.status) {
              for (var i = 0; i < $(".one_choose").length; i++) {
                if ($(".one_choose")[i].checked) {
                  $($(".item_detail")[i]).remove()
                }
              }
              this.cartEmpty()
              this.carttotal()
            }
          })
        }

      }
    }

    if (target.classList.contains("cart_sum_to_order")) {
      $(".pay_bg").css("display", "block")
    }

  })

  $(".pay_box span").click(() => {
    console.log(123);
    $(".pay_bg").css("display", "none")
  })

  //输入数字改变值
  $(".cart_item").change((e) => {
    console.log(123);
    var target = e.target
    if (target.classList.contains("num")) {
      let regnum = /^-?[1-9]\d*$/
      let oldNum = $(target).attr("index")
      let newNum = $(target).val()
      // console.log($(target).attr("index"));
      if (!(regnum.test(newNum)) || newNum > 5) {
        newNum = $(target).attr("index")
        $(target).val(newNum)
      } else {
        addcart({
          tellnumber: this.tellnumber,
          num: newNum - oldNum,
          id: $(target).attr("goodsId")
        }).then(res => {
          console.log(res);
          $(target).val(newNum)
          $(target).attr("index", newNum)
          this.carttotal()
        })
      }


    }
  })
}

cartFn.prototype.carttotal = function () {
  let numSum = 0
  let moneySum = 0

  for (var i = 0; i < $(".one_choose").length; i++) {
    //小计改变
    const ctotle = $($(".num")[i]).val() * $($(".price")[i]).html() * 1
    $($(".item_detail .cart_total")[i]).html(ctotle)
    if ($(".one_choose")[i].checked) {
      numSum += $($(".num")[i]).val() * 1
      moneySum += $($(".item_detail .cart_total")[i]).html() * 1
    }
  }
  $(".total_num").html(numSum)
  $(".total_price").html("￥" + moneySum)
}

cartFn.prototype.cartEmpty = function () {
  if ($(".one_choose").length == 0) {
    $(".cart_body").css("display", "none")
    $(".cart_empty").css("display", "block")
  } else {
    $(".cart_body").css("display", "block")
    $(".cart_empty").css("display", "none")
  }
}
new cartFn()